<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="format-detection" content="telephone=no">
    <link rel="stylesheet" href="/plugins/layui/css/layui.css" media="all"/>
    <link rel="stylesheet" href="/css/global.css" media="all">
    <link rel="stylesheet" href="/plugins/font-awesome/css/font-awesome.min.css">
    <link rel="stylesheet" href="/css/table.css"/>
    <title>知识讲堂管理</title>
    <style type="text/css">
        .searcheBottom{margin-bottom: 10px;}
        body{overflow-y: scroll;}
    </style>
</head>
<body>
<div class="admin-main">
    <blockquote class="layui-elem-quote">
        <div class="demoTable">
            <div class="layui-inline searcheBottom">
                文章名称：
                <div class="layui-inline">
                    <input id="articleTitle" class="layui-input" name="articleTitle" autocomplete="off" placeholder="文章名称"/>
                </div>
            </div>
            <div class="layui-inline searcheBottom">
                文章状态：
                <div class="layui-inline">
                    <select  id="enableFlag" name="enableFlag" class="layui-input" style="padding: 0px 20px;">
                        <option value="">全部</option>
                        <option value="1">已发布</option>
                        <option value="0">已下架</option>
                    </select>
                </div>
            </div>
            <div class="layui-inline searcheBottom">
                <div class="layui-inline">
                    <label class="layui-form-label">发布时间:</label>
                    <div class="layui-input-inline">
                        <input id="startTime" class="layui-input" name="startTime" placeholder="开始日">
                    </div>
                    <div class="layui-input-inline">
                        &nbsp;-&nbsp;
                    </div>
                    <div class="layui-input-inline">
                        <input id="endTime" class="layui-input" name="endTime" placeholder="结束日">
                    </div>
                </div>
            </div>
            <div class="layui-inline searcheBottom">
                <button class="layui-btn" data-type="reload" id="search" >搜索</button>
                <a class="layui-btn layui-btn-primary" id="reset">重置</a>
                <a data-opt="add" href="/manage/kroom/addKlroom" class="layui-btn"><i class="layui-icon">&#xe608;</i>新增知识讲堂</a>
            </div>
        </div>
    </blockquote>
    <span style="font-size: 16px;"><b>会员列表</b></span>
    <table class="layui-hide" id="kroomTable" lay-filter="tableFilter"></table>
</div>
<script type="text/javascript" src="/plugins/layui/layui.js"></script>
<script>
    layui.use(['jquery','table','laydate','layer','form','util'],function(){
        var $=layui.jquery,table=layui.table,laydate = layui.laydate,
            layer = layui.layer,form = layui.form,util=layui.util;

        pageInit();
        function pageInit() {
            //表格渲染
            table.render({
                elem:"#kroomTable",
                url:"/manage/kroom/klroomList",
                method:"post",
                cols:[[
                    {field:'id', title: 'ID', sort: true,align:'center'},
                    {field:'articleTitle', title: '文章名称',align:'center'},
                    {field:'enableFlag', title: '文章状态',align:'center',
                        templet: function(d){
                            if(d.enableFlag==true){
                                return '<div class="layui-btn layui-bg-green layui-btn-xs">已发布</div>'
                            } else{
                                return '<div class="layui-btn layui-bg-gray layui-btn-xs">已下架</div>'
                            }
                        }
                    },
                    {field:'setTop', title: '是否置顶',align:'center',
                        templet: function(d){
                            if(d.setTop==true){
                                return '<div class="layui-btn layui-bg-green layui-btn-xs">是</div>'
                            } else{
                                return '<div class="layui-btn layui-bg-gray layui-btn-xs">否</div>'
                            }
                        }
                    },
                    {field:'createTime', title: '提交时间',sort: true,align:'center',
                        templet: function(d){
                            if(d.createTime!=null){
                                return util.toDateString(d.createTime, 'yyyy-MM-dd HH:mm:ss');
                            }else{
                                return "";
                            }
                        }
                    },
                    {field:'publishTime', title: '发布时间',sort: true,align:'center',
                        templet: function(d){
                            if(d.publishTime!=null){
                                return util.toDateString(d.publishTime, 'yyyy-MM-dd HH:mm:ss');
                            }else{
                                return "";
                            }
                        }
                    },
                    {field: 'tool', title:"操作",width:300,align:'center',
                        templet:function(d){
                            var tool = '<a href="#" class="layui-btn layui-btn-xs" lay-event="edit">编辑</a>';
                            if(d.enableFlag==true){
                                tool+='<a href="#" class="layui-btn layui-btn-warm layui-btn-xs" lay-event="enable">下架</a>';
                            }else{
                                tool+='<a href="#" class="layui-btn layui-btn-normal layui-btn-xs" lay-event="enable">上架</a>';
                            }
                            if(d.setTop==true){
                                tool+='<a href="#" class="layui-btn layui-btn-warm layui-btn-xs" lay-event="top">取消置顶</a>';
                            }else{
                                tool+='<a href="#" class="layui-btn layui-btn-normal layui-btn-xs" lay-event="top">设为置顶</a>';
                            }
                            return tool;
                        }
                    }
                ]],
                limits:[10,15,20,25,30],
                limit:10,
                page:true
            });
        }

        //时间选择器
        var start = {
            max: '2099-06-16 23:59:59',
            format: 'yyyy-MM-dd HH:mm:ss',
            type: 'datetime',
            value: new Date(),
            show: true
            ,done: function(value){
                end.min = value; //开始日选好后，重置结束日的最小日期
                end.start = value //将结束日的初始值设定为开始日
            }
        };
        var end = {
            max: '2099-06-16 23:59:59',
            format: 'yyyy-MM-dd HH:mm:ss',
            type: 'datetime',
            value: new Date(),
            show: true
            ,done: function(value){
                start.max = value; //结束日选好后，重置开始日的最大日期
            }
        };
        $('#startTime').bind('click',function (){
            start.elem = this;
            start.closeStop = this
            laydate.render(start);
        });
        $('#endTime').bind('click',function (){
            end.elem = this;
            end.closeStop = this
            laydate.render(end);
        });

        //重置
        $("#reset").on("click",function(){
            articleTitle.value = '';
            enableFlag.value='';
            startTime.value = '';
            endTime.value = '';
        });

        //表格重载
        var active = {
            reload: function () {
                var articleTitle = $('#articleTitle');
                var enableFlag = $('#enableFlag option:selected');
                var startTime = $('#startTime');
                var endTime = $('#endTime');
                table.reload('kroomTable', {
                    where: {
                        articleTitle: articleTitle.val(),
                        enableFlag:enableFlag.val(),
                        startTime:startTime.val(),
                        endTime:endTime.val()
                    }
                });
            }
        };
        //将搜索框的值随请求传到后台
        $('#search').on('click', function () {
            var type = $(this).data('type');
            active[type] ? active[type].call(this) : '';
        });

        $(".layui-input").keydown(function (e) {
            if(e.keyCode == 13){
                $('#search').click();
            }
        });

        //表格监听
        table.on('tool(tableFilter)', function(obj){
            var data = obj.data;
            if (obj.event == "enable") {
                $.ajax({
                    url:"/manage/kroom/enableKlroom",
                    type:"post",
                    dataType:"json",
                    data:{"id":data.id,"enableFlag":data.enableFlag},
                    success:function(count){
                        table.reload("kroomTable");
                    }
                })
            } else if (obj.event == "edit") {
                location.href='/manage/kroom/editKlroom?id='+data.id+'';
            }
            else if (obj.event == "top") {
                $.ajax({
                    url:"/manage/kroom/topKlroom",
                    type:"post",
                    dataType:"json",
                    data:{"id":data.id,"setTop":data.setTop},
                    success:function(count){
                        table.reload("kroomTable");
                    }
                })
            }
        });
    })
</script>
</body>
</html>